package com.vencent.spring.web;

import javax.servlet.http.Part;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import com.vencent.spring.model.Students;

/**
 * test this program 's running.
 * @author 1d
 *
 */
@Controller
@RequestMapping("/")
public class HomeController {
	
	@RequestMapping(method = RequestMethod.GET)
	public String toHome(){
		return "home";
	}
	
	/**
	 * if binary data nothing to upload , the byte[] 's size is 0, but it 's not null;
	 * @param profileImg
	 * @param stu
	 * @param errors
	 * @return
	 */
	@RequestMapping(value = "register", method = RequestMethod.POST)
	public String register(@RequestPart byte[] profileImg, @Valid Students stu, Errors errors){
		return "show";
	}
	
	/**
	 * @param profileImg
	 * @param stu
	 * @param errors
	 * @return
	 */
	@RequestMapping(value = "registerMultipart", method = RequestMethod.POST)
	public String registerMultipart(MultipartFile profileImg, @Valid Students stu, Errors errors){
		/**
		 * this spring in action's version is lowwer, the new API changes a lot.
		 */
		return "show";
	}
	
	
	/**
	 * it seemly like multipartFile, maybe java imitate Spring, 
	 * spring's multipartFile transferTo(), here is write()
	 * spring's multipartFile getOriginalFilename() , here is getSubmittedFileName()
	 * of course, if you use Part + @RequestPart, you don't need MultipartResolver! 
	 * Only using MultipartFile, you need config MultipartResolver.
	 * @param profileImg
	 * @param stu
	 * @param errors
	 * @return
	 */
	@RequestMapping(value = "registerServletpart", method = RequestMethod.POST)
	public String registerServletPart(@RequestPart Part profileImg, @Valid Students stu, Errors errors){
		return "show";
	}
	
	
}
